﻿using System;
using System.Globalization;
using Business;
using System.Web.UI.WebControls;
using System.Linq;
using System.Collections.ObjectModel;
using Entities;

namespace ServicesDirectory.UserControl
{
    public partial class ListOrganisations : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData("All", true);
            }
            cmdBackAll.Click += BackAll_Click;
            cmdBackList.Click += BackList_Click;
            cmdNext.Click += Next_Click;
            cmdNextAll.Click += NextAll_Click;
            lbtAll.Click += lbtAll_Click;
            lbt09.Click += Lbt09_Click;
            lbtA.Click += LbtA_Click;
            lbtF.Click += LbtF_Click;
            lbtK.Click += LbtK_Click;
            lbtO.Click += LbtO_Click;
            lbtS.Click += LbtS_Click;
            lbtW.Click += LbtW_Click;
            lbtOrderOrgName.Click += lbtOrderOrgName_Click;
            lbtAddress.Click += LbtAddress_Click;
            lbtPostCode.Click += LbtPostCode_Click;
            lbtContact.Click += LbtContact_Click;
            chkInActive.CheckedChanged += ChkInActive_CheckedChanged;
        }

        void LbtContact_Click(object sender, EventArgs e)
        {
            OrganisationOrderByContact(string.IsNullOrEmpty(lbtAll.CommandArgument) ? "All" : lbtAll.CommandArgument);
        }

        void LbtPostCode_Click(object sender, EventArgs e)
        {
            OrganisationOrderByPostCode(string.IsNullOrEmpty(lbtAll.CommandArgument) ? "All" : lbtAll.CommandArgument);
        }

        void LbtAddress_Click(object sender, EventArgs e)
        {
            OrderOrganisationAddress(
                string.IsNullOrEmpty(lbtAll.CommandArgument) ? "All" : lbtAll.CommandArgument,
                !chkInActive.Checked);
        }

        void ChkInActive_CheckedChanged(object sender, EventArgs e)
        {
            BindData(string.IsNullOrEmpty(lbtAll.CommandArgument) ? "All" : lbtAll.CommandArgument,
                 !chkInActive.Checked);
        }

        void lbtOrderOrgName_Click(object sender, EventArgs e)
        {
            OrderOrganisationName(
                string.IsNullOrEmpty(lbtAll.CommandArgument) ? "All" : lbtAll.CommandArgument,
                !chkInActive.Checked);
        }

        protected void RptOrganisation_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var lblIsActive = e.Item.FindControl("lblIsactive") as Label;
            var lblPostCode = e.Item.FindControl("lblPostCode") as Label;
            var lblContact = e.Item.FindControl("lblContactName") as Label;
            if (lblIsActive != null)
                lblIsActive.Text = lblIsActive.Text == "True" ? "Yes" : "No";
            if (lblPostCode != null)
                lblPostCode.Text = AddressBusiness.Instance.GetAddress(lblPostCode.Text).PostCode;
            if (lblContact != null)
            {
                string message;
                var contact = ContactBusiness.Instance.GetContact(lblContact.Text, out message);
                lblContact.Text = contact.FirstName + " " + contact.SurName;
            }
        }

        void LbtW_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "wz";
            BindData(lbtAll.CommandArgument, !chkInActive.Checked);
        }

        void LbtS_Click(object sender, EventArgs e)
        {

            lbtAll.CommandArgument = "sv";
            BindData(lbtAll.CommandArgument, !chkInActive.Checked);
        }

        void LbtO_Click(object sender, EventArgs e)
        {

            lbtAll.CommandArgument = "or";
            BindData(lbtAll.CommandArgument, !chkInActive.Checked);
        }

        void LbtK_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "kn";
            BindData(lbtAll.CommandArgument, !chkInActive.Checked);
        }

        void LbtF_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "fj";
            BindData(lbtAll.CommandArgument, !chkInActive.Checked);
        }

        void LbtA_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "ae";
            BindData(lbtAll.CommandArgument, !chkInActive.Checked);
        }

        void Lbt09_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "09";
            BindData(lbtAll.CommandArgument, !chkInActive.Checked);
        }

        void lbtAll_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "All";
            BindData(lbtAll.CommandArgument, !chkInActive.Checked);
        }

        void NextAll_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = lblTotalPage.Text;
            BindData(lbtAll.CommandArgument, !chkInActive.Checked);
        }

        void Next_Click(object sender, EventArgs e)
        {
            if ((int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture) + 1) <= ((int.Parse(lblTotalPage.Text, CultureInfo.InvariantCulture))))
            {
                txtPageNumber.Text = (int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture) + 1).ToString(CultureInfo.InvariantCulture);
                BindData(lbtAll.CommandArgument, !chkInActive.Checked);


            }
        }

        

        void BackList_Click(object sender, EventArgs e)
        {
            if ((int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture) - 1) > 0)
            {
                txtPageNumber.Text = (int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture) - 1).ToString(CultureInfo.InvariantCulture);
                BindData(lbtAll.CommandArgument, !chkInActive.Checked);
            }
        }

        void BackAll_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            BindData(lbtAll.CommandArgument, !chkInActive.Checked);


        }

        private void BindData(string keyword, bool isActive)
        {
            var lstOrg = OrganizationBusiness.Instance.ListOrganisation(keyword, isActive);
            if (lstOrg != null)
            {
                lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                rptOrganisation.DataBind();
            }

        }
        private void OrderOrganisationName(string keyword, bool isActive)
        {
            var lstOrg = OrganizationBusiness.Instance.ListOrganisation(keyword, isActive);
            if (string.IsNullOrEmpty(lbtOrderOrgName.CommandArgument) || lbtOrderOrgName.CommandArgument == "ASC")
            {
                lstOrg = new Collection<Organisation>(lstOrg.OrderBy(c => c.OrgName).ToList());
                lbtOrderOrgName.CommandArgument = "DESC";
            }
            else
            {
                lbtOrderOrgName.CommandArgument = "ASC";
                lstOrg = new Collection<Organisation>(lstOrg.OrderByDescending(c => c.OrgName).ToList());
            }

            if (lstOrg != null)
            {
                lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                rptOrganisation.DataBind();
            }

        }
        private void OrganisationOrderByContact(string keyword)
        {

            if (string.IsNullOrEmpty(lbtContact.CommandArgument) || lbtContact.CommandArgument == "ASC")
            {
                var lstOrg = OrganizationBusiness.Instance.OrganisationOrderByContact(keyword, true);
                if (lstOrg != null)
                {
                    lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                    rptOrganisation.DataBind();
                }
                lbtContact.CommandArgument = "DESC";
            }
            else
            {
                lbtContact.CommandArgument = "ASC";
                var lstOrg = OrganizationBusiness.Instance.OrganisationOrderByContact(keyword, false);
                if (lstOrg != null)
                {
                    lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                    rptOrganisation.DataBind();
                }
            } 

        }
        private void OrganisationOrderByPostCode(string keyword)
        {

            if (string.IsNullOrEmpty(lbtPostCode.CommandArgument) || lbtPostCode.CommandArgument == "ASC")
            {
                var lstOrg = OrganizationBusiness.Instance.OrganisationOrderByAddress(keyword, true);
                if (lstOrg != null)
                {
                    lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                    rptOrganisation.DataBind();
                }
                lbtPostCode.CommandArgument = "DESC";
            }
            else
            {
                lbtPostCode.CommandArgument = "ASC";
                var lstOrg = OrganizationBusiness.Instance.OrganisationOrderByAddress(keyword, false);
                if (lstOrg != null)
                {
                    lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                    rptOrganisation.DataBind();
                }
            }

        }
        private void OrderOrganisationAddress(string keyword, bool isActive)
        {
            var lstOrg = OrganizationBusiness.Instance.ListOrganisation(keyword, isActive);
            if (string.IsNullOrEmpty(lbtAddress.CommandArgument) || lbtAddress.CommandArgument == "ASC")
            {
                lstOrg = new Collection<Organisation>(lstOrg.OrderBy(c => c.AddressLine1).ToList());
                lbtAddress.CommandArgument = "DESC";
            }
            else
            {
                lbtAddress.CommandArgument = "ASC";
                lstOrg = new Collection<Organisation>(lstOrg.OrderByDescending(c => c.AddressLine1).ToList());
            }

            if (lstOrg != null)
            {
                lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                rptOrganisation.DataBind();
            }

        }
        protected void rptOrganisation_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                var lblIsactive = e.Item.FindControl("lblIsactive") as Label;
                if (lblIsactive != null)
                    if (lblIsactive.Text == "Yes")
                    {
                        Response.Redirect("OrganisationDetail.aspx?orgId=" + e.CommandArgument);
                    }
                    else
                    {
                        Response.Write("<script type=\"text/javascript\">");
                        Response.Write("var result= window.confirm('Do you want to make this Organization active?'); ");
                        Response.Write("if(result)");
                        Response.Write(" location.href='OrganisationDetail.aspx?orgId=" + e.CommandArgument + "&isActive=Yes';");
                        Response.Write("</script>");
                    }
            }

        }

        protected void cmdCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("OrganisationDetail.aspx");
        }


    }
}